<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>文本交错动画</title>
  <style>
    body {
      font-family: 'Montserrat', sans-serif;
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
      background: #f8f9fa;
      margin: 0;
    }
    .container {
      max-width: 800px;
      padding: 2rem;
    }
    #text {
      font-size: 2.5rem;
      font-weight: 700;
      line-height: 1.3;
    }
    .char {
      display: inline-block;
      position: relative;
    }
    .btn {
      padding: 0.8rem 1.5rem;
      background: #6c757d;
      color: white;
      border: none;
      border-radius: 4px;
      cursor: pointer;
      font-size: 1rem;
      margin-top: 2rem;
      transition: all 0.3s;
    }
    .btn:hover {
      background: #5a6268;
    }
  </style>
</head>
<body>
<div class="container">
  <div id="text">交错动画为每个字符添加不同效果</div>
  <button class="btn" id="replay">重新播放</button>
</div>

<!-- 无需手动注册，可直接使用 -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.11.4/gsap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gsap@3.13.0/dist/SplitText.min.js"></script>
<script>
  gsap.registerPlugin(SplitText);

  let split;
  let tl;

  function initAnimation() {
    if (split) split.revert();
    if (tl) tl.kill();

    // 分割文本
    split = new SplitText("#text", {
      type: "chars",
      charsClass: "char"
    });

    // 设置随机初始状态
    split.chars.forEach((char, i) => {
      gsap.set(char, {
        opacity: 0,
        y: gsap.utils.random(-50, 50),
        x: gsap.utils.random(-30, 30),
        rotation: gsap.utils.random(-30, 30)
      });
    });

    // 创建交错动画
    tl = gsap.timeline()
            .to(split.chars, {
              duration: 1,
              opacity: 1,
              y: 0,
              x: 0,
              rotation: 0,
              stagger: {
                amount: 1.5,
                from: "random"
              },
              ease: "elastic.out(1, 0.5)"
            });
  }

  document.addEventListener('DOMContentLoaded', initAnimation);
  document.getElementById("replay").addEventListener("click", initAnimation);
</script>
</body>
</html>